//
//  QuestionsAnswersViewController.m
//  HappyMother
//
//  Created by 易东 on 2017/10/25.
//  Copyright © 2017年 东哥. All rights reserved.
//

#import "QuestionsAnswersViewController.h"
#import "QuestionsAnswersCell.h"
#import "QuestionAnswerFiltrateView.h"
#import "QuestionAnswerDetailController.h"
#import "QuestionViewController.h"


#define imagePlayerH 166
#define durion 0.2

@interface QuestionsAnswersViewController ()<UIScrollViewDelegate,ImagePlayerViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)ImagePlayerView *imagePlayerView;

@property (nonatomic, strong) NSArray *imageURLs;



@end

@implementation QuestionsAnswersViewController
{
    UITableView *showTabelView;
    QuestionAnswerFiltrateView *questionAnswerFiltrateView;
    BOOL selectMenu;
    UIButton *_button;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"问答";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createNaviBarButton];
    
    _imageURLs = @[@"pic1.jpg",@"pic2.jpg",@"pic3.jpg",@"pic4.jpg"];
    
    [self createUI];
}
#pragma mark - 创建UI界面
-(void)createUI{
    
    
    showTabelView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    showTabelView.width = Screen_Width;
    showTabelView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    showTabelView.delegate = self;
    showTabelView.dataSource = self;
    [self.view addSubview:showTabelView];
    
    
    //广告位
    _imagePlayerView = [[ImagePlayerView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, imagePlayerH)];
    self.imagePlayerView.imagePlayerViewDelegate = self;

    self.imagePlayerView.pageControlPosition = ICPageControlPosition_BottomCenter;
    
    _imagePlayerView.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    _imagePlayerView.pageControl.pageIndicatorTintColor = [UIColor blackColor];
    
    // endless scroll
    self.imagePlayerView.endlessScroll = YES;
    self.imagePlayerView.scrollInterval = 2.5;
    [self.imagePlayerView reloadData];
    
    showTabelView.tableHeaderView = _imagePlayerView;
    
    //菜单筛选
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 30, 25);
    
    [btn setImage:[UIImage imageNamed:@"我的"] forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:@"我的选中"] forState:UIControlStateHighlighted];
    
    [btn setContentMode:UIViewContentModeCenter];
    
    [btn addTarget:self action:@selector(nemuAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
   
    //提问按钮
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _button.backgroundColor = [UIColor grayColor];
    
    _button.frame = CGRectMake(Screen_Width-80, Screen_Height-150, 60, 60);
    
    _button.backgroundColor = [UIColor brownColor];
    _button.alpha = 0.9988888;
    _button.layer.cornerRadius = 60/2.0;
    _button.clipsToBounds = YES;
    [_button setTitle:@"提问" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [_button addTarget:self action:@selector(choose) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_button];
    
    //放一个拖动手势，用来改变控件的位置
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];
    
    [_button addGestureRecognizer:pan];
    
    
    
}
#pragma mark -提问
-(void)choose
{
    
    QuestionViewController *detailvc = [[QuestionViewController alloc] init];
    
    [self.navigationController pushViewController:detailvc animated:YES];
}
#pragma mark -手势事件 －－ 改变位置
-(void)changePostion:(UIPanGestureRecognizer *)pan

{
    
    CGPoint point = [pan translationInView:self.view];

    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    
    CGRect originalFrame = _button.frame;
    
    if (originalFrame.origin.x >= 0 && originalFrame.origin.x+originalFrame.size.width <= width) {
        
        originalFrame.origin.x += point.x;
        
    }
    
    if (originalFrame.origin.y >= 0 && originalFrame.origin.y+originalFrame.size.height <= height) {
        
        originalFrame.origin.y += point.y;
        
    }
    
    _button.frame = originalFrame;
    
    

    [pan setTranslation:CGPointZero inView:self.view];
    
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        
        _button.enabled = NO;
        
        
    }else if (pan.state == UIGestureRecognizerStateChanged){
        
        [self buttonFrame];
        
        
    } else {
        
        [self buttonFrame];
        
    }
    
}
-(void)buttonFrame
{
    CGRect frame = _button.frame;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    //记录是否越界
    
    BOOL isOver = NO;
    
    
    if (frame.origin.x < 0) {
        
        frame.origin.x = 0;
        
        isOver = YES;
        
    } else if (frame.origin.x+frame.size.width > width) {
        
        frame.origin.x = width - frame.size.width;
        
        isOver = YES;
        
    }

    if (frame.origin.y < 0) {
        
        frame.origin.y = 0;
        
        isOver = YES;
       //有一个状态栏和导航栏的高度
    } else if (CGRectGetMaxY(_button.frame)>height-65){
        
        frame.origin.y = height - _button.height-65;
        
        isOver = YES;
        
    }
    
    if (isOver) {
        
        [UIView animateWithDuration:0.3 animations:^{
            
            _button.frame = frame;
            
        }];
        
    }
    
    _button.enabled = YES;
}
#pragma mark - 菜单筛选
-(void)nemuAction:(UIButton *)btn
{
    
    if (!questionAnswerFiltrateView) {
        questionAnswerFiltrateView = [[[NSBundle mainBundle] loadNibNamed:@"QuestionAnswerFiltrateView" owner:nil options:nil] firstObject];
        questionAnswerFiltrateView.frame = CGRectMake(0, -Screen_Height, Screen_Width,Screen_Height);
        
        [self.view addSubview:questionAnswerFiltrateView];
        
         __weak typeof(self) weakself = self;
   
        questionAnswerFiltrateView.cancelBlock = ^(id object) {
            
            [weakself cancel];
            
        };
        
        questionAnswerFiltrateView.ensureBlock = ^(id object) {
            
            [weakself ensure:object];
            
        };
        
    }
    
    selectMenu = !selectMenu;
    
    
    questionAnswerFiltrateView.touchView.backgroundColor = [UIColor whiteColor];
    
    
    
    [UIView animateWithDuration:durion animations:^{
    
        
        
     if (selectMenu) {
         
           questionAnswerFiltrateView.frame = CGRectMake(0, 0, Screen_Width,Screen_Height);
         
     }else{
         
          questionAnswerFiltrateView.frame = CGRectMake(0, -Screen_Height, Screen_Width,Screen_Height);
         
     }
      
        
    } completion:^(BOOL finished) {
    
        questionAnswerFiltrateView.touchView.backgroundColor = [UIColor blackColor];
        
        if (!selectMenu) {
            questionAnswerFiltrateView.touchView.backgroundColor = [UIColor whiteColor];
            [questionAnswerFiltrateView removeFromSuperview];
            questionAnswerFiltrateView = nil;
        }
        
    }];
    
}
#pragma mark - 取消菜单
-(void)cancel{
    
    selectMenu = NO;
    
    questionAnswerFiltrateView.touchView.backgroundColor = [UIColor whiteColor];
    
    [UIView animateWithDuration:durion animations:^{
        questionAnswerFiltrateView.frame = CGRectMake(0, -Screen_Height, Screen_Width,Screen_Height);
        
    } completion:^(BOOL finished) {
    
        [questionAnswerFiltrateView removeFromSuperview];
        questionAnswerFiltrateView = nil;
        
        
    }];
    
}
#pragma mark - 确认菜单
-(void)ensure:(id)obj
{
    [self cancel];
}
#pragma mark - ImagePlayerViewDelegate
- (NSInteger)numberOfItems
{
    return _imageURLs.count;
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index
{
    NSString *imagename = _imageURLs[index];
    
    [imageView setImage:[UIImage imageNamed:imagename]];
    
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index
{
    

}

#pragma mark --- 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *_indetity = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_indetity];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QuestionsAnswersCell" owner:nil options:nil] firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuestionAnswerDetailController *detailvc = [[QuestionAnswerDetailController alloc] init];
    
    [self.navigationController pushViewController:detailvc animated:YES];
    
}
@end
